function IsNumeric(valeur) {
   return (valeur - 0) == valeur && valeur.length > 0;
}

function clrz_body_class() {
    bod = document.body.className.split(' ');
    var nav = navigator.userAgent.toLowerCase();

	// Detect capacities
	if (window.Touch) bod.push('is_touchscreen');

	// Detect FFx
    if(nav.match(/firefox/gi)) bod.push('is_firefox');

	// Detect Webkit
    if(nav.match(/AppleWebKit/gi)) bod.push('is_webkit');

	// Detect Chrome
    if(nav.match(/Chrome/gi)) bod.push('is_chrome');

	// Detect Opera
    if(nav.match(/opera/gi)) bod.push('is_opera');

	// Detect iOS
    if(nav.match(/ipod/gi) || nav.match(/iphone/gi) || nav.match(/ipad/gi)) bod.push('is_ios');
    if(nav.match(/ipod/gi)) bod.push('is_ipod');
    if(nav.match(/iphone/gi)) bod.push('is_iphone');
    if(nav.match(/ipad/gi)) bod.push('is_ipad');

    document.body.className = bod.join(' ');
}


function clrz_placeholder(input){
    if (!input || input.getProperty('placeholder') === '' || ("placeholder" in document.createElement("input"))) return null;
    var placeholder = input.getProperty('placeholder');
    input.erase('placeholder');

    if(input.tagName == 'TEXTAREA')
        input.setProperty('value', input.get('html'));

    var is_password = (input.type == 'password');
    if(is_password) input.setProperty('type', 'text');

    if(input.getProperty('value') === '')
        input.setProperty('value', placeholder).addClass('defaultvalue');

    input.addEvents({
        'focus':function(e){
            if(input.getProperty('value') == placeholder){
                input.setProperty('value', '');
                input.removeClass('defaultvalue');
                if(is_password) input.setProperty('type', 'password');
            }
        },
        'blur':function(e){
            if(input.getProperty('value') === ''){
                input.addClass('defaultvalue');
                if(is_password) input.setProperty('type', 'text');
                input.setProperty('value', placeholder);
            }
        }
    });
}

function clrz_same_size(cible){
    var max_height = 0;
    $$(cible).each(function(el){
        var tmp_height = el.getHeight() - el.getStyle('padding-top').toInt() - el.getStyle('padding-bottom').toInt();
        max_height = Math.max(max_height,tmp_height);
    });
    $$(cible).each(function(el){
        var myEffect = new Fx.Morph(el, {
            duration: '50',
            transition: Fx.Transitions.Sine.easeOut
        });
        myEffect.start({'height':max_height});
    });
}

function clrz_comments_parents(){
    if(!$('dont_reply_to_comment') || !$('txt_add_your_comment') || !$('txt_reply_to_comment') || !$('add_your_comment')) return;
    $$('.clrz_click_comments_form').each(function(e){
        e.addEvent('click',function(){
            $('clrz_comment_parent').set('value',e.get('rel'));
            $('dont_reply_to_comment').setStyles({'display':''});
            $('add_your_comment').set('html',$('txt_reply_to_comment').getAttribute('value'));
        });
    });
    $('dont_reply_to_comment')
        .setStyles({'display':'none'})
        .addEvent('click',function(e){
            e.stop();
            this.setStyles({'display':'none'});
            $('clrz_comment_parent').set('value','');
            $('add_your_comment').set('html',$('txt_add_your_comment').getAttribute('value'));
        });


}


/* ANCRE */
function initSmoothScroll(linkancre) {
    $$(linkancre).each(function(el,i){
        el.addEvents({
            'click': function(e){
                e.stop();
                var target = el.getProperty('href');
                var divToScrollTo = target.split('#')[1];
                if($(divToScrollTo)){
                    new Fx.Scroll(window, {
                            duration: 700
                    }).toElement($(divToScrollTo));
                }
            }
        });
    });
}

function ios_fixes() {
    var user_agent = navigator.userAgent.toLowerCase();
    if (user_agent.match(/ipad/gi) || user_agent.match(/ipod/gi) || user_agent.match(/iphone/gi)) {
        $(document.body).setStyles({
            '-webkit-text-size-adjust': 'none'
        });
        setTimeout(function() {
            window.scrollTo(0, 1);
        }, 50);
    }
}

/* ----------------------------------------------------------
    Touchmove
   ------------------------------------------------------- */

function clrzSetTouchMove(el) {

    el.addEvent('touchstart', function(e) {
        // e.preventDefault();
        var etouches = e.touches || e.changedTouches,
            touch = etouches[0];

        if(etouches.length > 1)
            return;

        this.store('touchx', touch.pageX);
        this.store('touchy', touch.pageY);
        this.store('eventLaunchable', 1);
    });

    el.addEvent('touchend', function(e) {
        // e.preventDefault();
        var touch = e.touches[0] || e.changedTouches[0];
        this.store('touchx', touch.pageX);
        this.store('touchy', touch.pageY);
        this.store('eventLaunchable', 1);
    });

    el.addEvent('touchmove', function(e) {
        var _this = this,
            pas = 25,
            origX = _this.retrieve('touchx'),
            origY = _this.retrieve('touchy'),
            eventLaunchable = _this.retrieve('eventLaunchable'),
            touch = e.touches[0] || e.changedTouches[0];

        // Annuler si event non launchable
        if (eventLaunchable !== 1) {
            return;
        }

        // Top
        if (origY - pas > touch.pageY) {
            e.preventDefault();
            _this.store('eventLaunchable', 0);
            _this.fireEvent('swiftTop');
        }

        // Right
        if (origX + pas < touch.pageX) {
            e.preventDefault();
            _this.store('eventLaunchable', 0);
            _this.fireEvent('swiftRight');
        }

        // Bottom
        if (origY + pas < touch.pageY) {
            e.preventDefault();
            _this.store('eventLaunchable', 0);
            _this.fireEvent('swiftBottom');
        }

        // Left
        if (origX - pas > touch.pageX) {
            e.preventDefault();
            _this.store('eventLaunchable', 0);
            _this.fireEvent('swiftLeft');
        }

    });
}
function sliderFooter() {
    if ($("slider-footer")) {
        var sliderFooter = new clrzSlider({
            perpage:5,
            // containerwidth:0,
            container:'slider-footer',
            items:'#slider-footer .slider-item',
            contentslider:'slider-footer__inner',
            btnprev:'#nav-footer__left',
            btnnext:'#nav-footer__right',
            bordertransition:'',
            marge:5
        });
    }
}

function sliderRelated() {
    if($$('.block-slider__inner').length === 0)
        return;
    $$('.block-related').each(function(el,i){
        var relativeel = el.getElements('.block-slider__inner')[0];
        if(relativeel && $(relativeel.getProperty('id'))){
            var absoluteel = el.getElements('.slider-bullet')[0];
            var prevel = el.getElements('.sb-nav.is-left')[0];
            var nextel = el.getElements('.sb-nav.is-right')[0];
            var sliderRelated = new clrzSlider({
                perpage:3,
                // containerwidth:0,
                container:relativeel.getProperty('id'),
                items:'#'+relativeel.getProperty('id')+' .slider-item',
                contentslider:absoluteel.getProperty('id'),
                btnprev:'#'+prevel.getProperty('id'),
                btnnext:'#'+nextel.getProperty('id'),
                bordertransition:'',
                marge:5
            });
        }
    });
}

function sliderInstagram() {
    if ($("slider-container__instagram")) {

        var periodicalID;


        var sliderInstagram = new clrzSlider({
            container:'slider-container__instagram',
            items:'.slider-inner__instagram--item',
            contentslider:'slider-inner__instagram',
            btnprev:'',
            btnnext:'',
            bordertransition:'',
            onZgoto:function(){
                var sliderobj = this;
                $clear(periodicalID);
                periodicalID = (function() {
                    sliderobj.scrollmenext();
                }).periodical(5000);
                $$('#slider-nav__instagram .item').removeClass('is-active');
                $$('#slider-nav__instagram .item')[this.setpage].addClass('is-active');
            }
        });

        $$('#slider-nav__instagram .item').each(function(el,i){
            el.addEvents({
                'click':function(e){
                    if(e)
                        e.stop();
                    sliderInstagram.zgoto(i);
                }
            });
        });






    }
}

function sliderWishlist() {
    if ($("slider-container__wishlist")) {
        var sliderWishlist = new clrzSlider({
            // containerwidth:0,
            container:'slider-container__wishlist',
            items:'.slider-inner__wishlist--item',
            contentslider:'slider-inner__wishlist',
            btnprev:'#slider-wishlist__left',
            btnnext:'#slider-wishlist__right',
            bordertransition:''
        });
    }
}



function loadmore(){

    if($('ajax-pagination') && $('ajax-content')){

        var paginbloc = $('ajax-pagination');
        var contentbloc = $('ajax-content');

        $$('#ajax-pagination .link-more-articles').addEvents({
            'click':function(e){
                var btn = this;
                if(e)
                    e.stop();

                if(btn.hasClass('loading'))
                    return;

                var myRequest = new Request({
                    url: btn.getProperty('href'),
                    method:'post',
                    onRequest:function(){
                        btn.addClass('loading');
                    },
                    onComplete:function(response){
                        var removediv = new Element('div', {'id':'removediv'}).setStyles({'display':'none'}).set('html', response).inject($(document.body));
                        if(removediv.getElements('.ajax-content')[0])
                            contentbloc.set('html', contentbloc.get('html')+removediv.getElements('.ajax-content')[0].get('html'));
                        if(removediv.getElements('.ajax-pagination')[0])
                            paginbloc.set('html', removediv.getElements('.ajax-pagination')[0].get('html'));
                        else{
                            paginbloc.destroy();
                        }
                        removediv.destroy();
                        loadmore();
                        
                        if (typeof FB !== 'undefined' && FB.XFBML)
                            FB.XFBML.parse();
                        
                        pinitimgs();
                        
                    }
                }).send('ajax=1');
            }
        });


    }


}



function fixheader(){
    var topmargin = $('header').getTop();

    var checkposition = function(){
        if(window.getScroll().y-topmargin>=0){
            $(document.body).addClass('fixheader');
        }else{
            $(document.body).removeClass('fixheader');
        }
    }

    window.addEvents({
        'scroll':function(){
            checkposition();
        }
    });
    checkposition();


}




function fixsidebar(){

    if($('sidebar')){
        var sidebar = $('sidebar');

        if($('sidebar').getHeight()<$('content').getSize().y-40){

            var inittop = sidebar.getPosition($(document.body)).y;
            
            if($$('.main-container.has_big_post').length>0){
                var images = [$$('.post__img img')[0].getProperty('src')];
                new Asset.images(images, {
                    onComplete: function() {
                        inittop = sidebar.getPosition($(document.body)).y;
                    }
                });
            }
            sidebar.store('inittop', inittop);
            var checkpositionsidebar = function(){


                var bottomel = $('footer');
                if($$('.block-popular').length>0){
                    bottomel = $$('.block-popular')[0];
                }

                var bottomeltop = bottomel.getPosition().y;
                if(window.getScroll().y-inittop-sidebar.getSize().y+window.getHeight()>=0){
                    $(document.body).addClass('fixsidebar');
                    sidebar.setStyles({'left':$('content').getPosition($(document.body)).x+$('content').getWidth()+40});

                    var diffbottom = window.getScroll().y+window.getSize().y-bottomeltop;

                    if(diffbottom>0){
                        sidebar.setStyles({'bottom':diffbottom});
                    }else{
                        sidebar.setStyles({'bottom':0});
                    }



                }else{
                    $(document.body).removeClass('fixsidebar');
                    sidebar.setStyles({'left':'auto'});
                }
            }

            window.addEvents({
                'scroll':function(){
                    checkpositionsidebar();
                }
            });
            checkpositionsidebar();
        }
    }
}

var scrollSingle = new Class({
    Implements: [Options, Events],
    options: {
        elem: false,
        elemPos: 0,
        beginFixed: 0,
        postContentPosition: 0,
        postContentPositionEnd: 0
    },

    initialize: function(options) {
        var maClass = this;
        this.options.elem = $('share-fixed-single');
        this.options.elemPos = this.options.elem.getPosition().y;
        this.options.beginFixed = this.options.elem.getPosition().y + 24;
        this.options.postContentPositionX = $$(".post-content")[0].getPosition().x;
        this.options.postContentPositionY = $$(".post-content")[0].getPosition().y;
        this.options.postContentPositionEnd = $$(".post-content")[0].getSize().y;

        window.addEvent('domready', function() {
            maClass.options.elem
                .setStyles({
                    top: maClass.options.postContentPositionY,
                    left: maClass.options.postContentPositionX - 60
                })
        });

        window.addEvent('scroll', function() {
            if (maClass.options.elem) {
                maClass.scrollShareSingle();
            }
        });
    },

    scrollShareSingle: function(options) {
        var scrollPosition = window.getScroll().y;
        this.options.postContentPositionEnd = $$(".post-content")[0].getSize().y;

        if (scrollPosition >= this.options.postContentPositionEnd -95) {
            this.options.elem
                .setProperty("style", "")
                .setStyles({
                    position: "absolute",
                    top: this.options.postContentPositionEnd,
                    left: -60
                })

        } else {
            this.options.elem
                .setProperty("style", "")
                .setStyles({
                    top: this.options.postContentPositionY,
                    left: this.options.postContentPositionX - 60
                })
        }
    }

});



function pinitimgs(){
    
    $$('.loop-big img,.post-content img').each(function(el,i){
        if(el.getParent().hasClass('pintimg'))
            return;
        var divel = new Element('div', {'class':'pintimg'}).inject(el, 'before');
        el.inject(divel);
        var description = '';
        if(el.getProperty('title')!=''){
            description = el.getProperty('title');
        }else if(el.getProperty('alt')!=''){
            description = el.getProperty('alt');
        }
        var linkpintit = new Element('a', {'target':'_blank', 'href':'//www.pinterest.com/pin/create/button/?url='+el.getProperty('src')+'&media='+el.getProperty('src')+'&description='+description, 'data-pin-do':'buttonPin', 'data-pin-config':'none', 'class':'pinbutton'}).set('html', '<img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" />');
        linkpintit.inject(divel);
    });
    
    
//    (function(d){
//        var f = d.getElementsByTagName('SCRIPT')[0], p = d.createElement('SCRIPT');
//        p.type = 'text/javascript';
//        p.async = true;
//        p.src = '//assets.pinterest.com/js/pinit.js';
//        f.parentNode.insertBefore(p, f);
//    }(document));
    
    
    
}

